import ResManager from "../../core/ResManager";
import { ResType } from "../../net/Commond";
import Singleton from "../../net/Singleton";
import { Utils } from "../../utils/Utils";
import { ResPath } from "../data/CFCommon";
import BattleRoleUi from "../ui/battleUi/BattleRoleUi";

export class BattleAnim extends Singleton {
    //创建直接效果
    doCreatrHurtEffect(obj) {
        let nodeRoleDef = obj.nodeRoleDef;
        let ctrl = nodeRoleDef.getComponent(BattleRoleUi);
        let skillConfig = ResManager.ins().getRes('config_skill');
        let _config = skillConfig[obj.skillId];
        if (_config.hitAnim) {
            ctrl.addEffect(_config.hitAnim);
        }
    }

    //创建落雷类效果
    doCreatrInjectEffect(obj) {
        let nodeRoleAtk = obj.nodeRoleAtk
        let nodeRoleDef = obj.nodeRoleDef
        let ctrl = nodeRoleDef.getComponent(BattleRoleUi)
        let skillConfig = ResManager.ins().getRes('config_skill')
        let _config = skillConfig[obj.skillId]
        if (_config.dropAnim) {
            ctrl.addEffect(_config.dropAnim, true, nodeRoleAtk.x > nodeRoleDef.x ? -1 : 1)
        }
    }

    //创建飞行效果
    doCreatrFlightEffect(obj, isRnd, node) {
        let nodeRoleAtk = obj.nodeRoleAtk
        let nodeRoleDef = obj.nodeRoleDef
        let skillConfig = ResManager.ins().getRes('config_skill')
        let _config = skillConfig[obj.skillId]
        if (_config && _config.flyAnim) {
            this.onCreateSkillEffectArrow(_config.flyAnim, nodeRoleAtk, nodeRoleDef, obj.moveTime, obj.showTime, isRnd, node);
        }
    }

    //创建飞行道具-箭羽类
    onCreateSkillEffectArrow(prefabName, attacker_node, targer_node, time, showTime, rnd, node) {
        if (attacker_node && targer_node) {
            //创建飞行道具
            ResManager.ins().loadSingle(prefabName, ResType.prefab, ResPath.flyEffect, (asset) => {
                // console.log("prefabName===",prefabName)
                let newNode = new cc.Node()
                newNode.x = attacker_node.x
                newNode.y = attacker_node.y + 50
                node.addChild(newNode)
                newNode.scale = 0.6
                let _node = cc.instantiate(asset)
                newNode.addChild(_node)
                let deltaP = targer_node.position.sub(attacker_node.position)
                let angle2 = cc.v2(deltaP.x, deltaP.y).signAngle(cc.v2(1, 0)) / Math.PI * 180
                newNode.angle = -angle2
                _node.active = false
                if (rnd) {
                    let toAngle = 30
                    let toHeight = Utils.getRandomInt(-100, 100)
                    let action = cc.sequence(
                        cc.delayTime(showTime),
                        cc.callFunc(function () {
                            _node.active = true
                        }),
                        this.createPWX(time, newNode.position, cc.v2(targer_node.x, targer_node.y + 50), toHeight, toAngle),
                        cc.callFunc(function () {
                            newNode.destroy()
                        }))
                    newNode.runAction(action)
                } else {
                    let action = cc.sequence(
                        cc.delayTime(showTime),
                        cc.callFunc(function () {
                            _node.active = true
                        }),
                        cc.moveTo(time, targer_node.x, targer_node.y + 50),
                        cc.callFunc(function () {
                            newNode.destroy()
                        }))
                    newNode.runAction(action)
                }
                // if (this.step == BATTLESTEP.POWERSKILL) {
                //     newNode.pauseAllActions()
                //     let effectAnim = _node.getComponent(cc.Animation)
                //     if (effectAnim) { effectAnim.pause() }
                // }
            }, node);
        }
    }
    // 曲线配置
    createPWX(t, startPoint, endPoint, height, angle) {
        // 把角度转换为弧度
        let radian = angle * Math.PI / 180;
        // 第一个控制点为抛物线左半弧的中点
        let q1x = startPoint.x + (endPoint.x - startPoint.x) / 4;
        let q1 = cc.v2(q1x, height + startPoint.y + Math.cos(radian) * q1x);
        // 第二个控制点为整个抛物线的中点
        let q2x = startPoint.x + (endPoint.x - startPoint.x) / 2;
        let q2 = cc.v2(q2x, height + startPoint.y + Math.cos(radian) * q2x);
        // 曲线配置
        return cc.bezierTo(t, [q1, q2, endPoint]).easing(cc.easeInOut(0.5));
    }
}